#include <conio.h>
#include <stdlib.h>
#include <stdio.h>			//For some reason, stdio.h were not included, so I added it(TheCell)
							//See Explanation 1

#include "includes\al\al.h"
#include "includes\al\alc.h"
#include "includes\al\alut.h"		//Added by TheCell
							//See explanation 2

#define MAX_AUDIOSOURCES 4
/*
* These are OpenAL "names" (or "objects"). They store and id of a buffer
* or a source object. Generally you would expect to see the implementation
* use values that scale up from '1', but don't count on it. The spec does
* not make this mandatory (as it is OpenGL). The id's can easily be memory
* pointers as well. It will depend on the implementation.
*/

// Buffers to hold sound data.
ALuint Buffer[MAX_AUDIOSOURCES];

// Sources are points of emitting sound.
ALuint Source[MAX_AUDIOSOURCES];

/*
* These are 3D cartesian vector coordinates. A structure or class would be
* a more flexible of handling these, but for the sake of simplicity we will
* just leave it as is.
*/

// Position of the source sound.
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };

// Velocity of the source sound.
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };

// Position of the Listener.
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };

// Velocity of the Listener.
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };

// Orientation of the Listener. (first 3 elements are "at", second 3 are "up")
// Also note that these should be units of '1'.
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };

/*
* ALboolean LoadALData()
*
*	This function will load our sample data from the disk using the Alut
*	utility and send the data into OpenAL as a buffer. A source is then
*	also created to play that buffer.
*/
ALboolean LoadALData(int source, ALbyte *path)
{
	// Variables to load into.

	ALenum format;
	ALsizei size;
	ALvoid* data;
	ALsizei freq;
	ALboolean loop;

	// Load wav data into a buffer.

	alGenBuffers(1, &Buffer[source]);

	if(alGetError() != AL_NO_ERROR)
		return AL_FALSE;

	//alutLoadWAVFile("wav/spacesong.wav", &format, &data, &size, &freq, &loop);
	alutLoadWAVFile(path, &format, &data, &size, &freq, &loop);
	alBufferData(Buffer[source], format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);

	// Bind the buffer with the source.
	alGenSources(1, &Source[source]);

	if(alGetError() != AL_NO_ERROR)
		return AL_FALSE;

	alSourcei (Source[source], AL_BUFFER,   Buffer[source]  );
	alSourcef (Source[source], AL_PITCH,    1.0      );
	alSourcef (Source[source], AL_GAIN,     1.0      );
	alSourcefv(Source[source], AL_POSITION, SourcePos);
	alSourcefv(Source[source], AL_VELOCITY, SourceVel);
	alSourcei (Source[source], AL_LOOPING,  loop     );

	// Do another error check and return.

	if(alGetError() == AL_NO_ERROR)
		return AL_TRUE;

	return AL_FALSE;
}

/*
* void SetListenerValues()
*
*	We already defined certain values for the Listener, but we need
*	to tell OpenAL to use that data. This function does just that.
*/
void SetListenerValues()
{
	alListenerfv(AL_POSITION,    ListenerPos);
	alListenerfv(AL_VELOCITY,    ListenerVel);
	alListenerfv(AL_ORIENTATION, ListenerOri);
}

/*
* void KillALData()
*
*	We have allocated memory for our buffers and sources which needs
*	to be returned to the system. This function frees that memory.
*/
void KillALData()
{
	for(int i = 0; i < MAX_AUDIOSOURCES; i++)
	{
		alDeleteBuffers(1, &Buffer[MAX_AUDIOSOURCES]);
		alDeleteSources(1, &Source[MAX_AUDIOSOURCES]);
	}
	alutExit();
}

// --------------------------------------------------------------------
void playSound(int sound)
{
	alSourcePlay(Source[sound]);
}

// --------------------------------------------------------------------
void stopSound(int sound)
{
	alSourceStop(Source[sound]);
}

// --------------------------------------------------------------------
void init_openAL(void)
{
	// Initialize OpenAL and clear the error bit.
	alutInit(NULL, 0);
	alGetError();

	//// Load the wav data.
	//if(LoadALData() == AL_FALSE)
	//{
	//    printf("Error loading data.");
	//	//return 0;
	//}

	if(LoadALData(0, "wav/spacesong.wav") == AL_FALSE) printf("Error loading data.");
	LoadALData(1, "wav/bulletshot.wav");
	LoadALData(2, "wav/alienmove.wav");
	LoadALData(3, "wav/alienkill.wav");

	SetListenerValues();
	// Setup an exit procedure.

	atexit(KillALData);

	//// Init the game song
	//alSourcePlay(Source[0]);
}

// --------------------------------------------------------------------